package com.ja.openchat.ui.chat.adapter;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.ja.openchat.ui.OuterShareActivity;
import com.ja.openchat.ui.chat.PreviewWebViewActivity;
import com.zy.openchat.core.model.message.InlineKeyboardButton;

import java.util.List;

public class GroupAdAdapter extends BaseQuickAdapter<InlineKeyboardButton, BaseViewHolder> {

    public GroupAdAdapter(@Nullable List<InlineKeyboardButton> data) {
        super(R.layout.item_group_ad_info, data);
        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                InlineKeyboardButton button = getItem(position);
                String link = button.getUrl();
                if(!TextUtils.isEmpty(link)&&link.contains("link=")){
                    String linkContent = link.substring(link.indexOf("link=")+"link=".length());
                    if(!TextUtils.isEmpty(linkContent)){
                        OuterShareActivity.handleOuterInviteLink((FragmentActivity)getContext(), linkContent);
                        return;
                    }
                }
                PreviewWebViewActivity.loadUrl(getContext(),button.text,button.getUrl());
            }
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, InlineKeyboardButton item) {
        View adGroupContent = helper.getView(R.id.adGroupContent);
        adGroupContent.getBackground().setAlpha(60);
        helper.setText(R.id.adName,item.text);
    }
}
